/**
 * User: zsquirrel
 * Date: 2020/4/28
 * Time: 4:47 下午
 */
package com.cskaoyan.mall.dao;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.Admin;
import com.cskaoyan.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminDaoImpl implements AdminDao {
    @Override
    public int login(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            Admin ad = runner.query("select * from admin where email = ? and pwd = ?",
                    new BeanHandler<>(Admin.class),
                    admin.getEmail(),
                    admin.getPwd());
            if(ad != null){
                return 200;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 500;
    }

    @Override
    public List<Admin> allAdmins() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = runner.query("select * from admin", new BeanListHandler<Admin>(Admin.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    /**
     * 构建不同的sql语句
     * @param params
     * @return
     */
    @Override
    public List<Admin> getSearchAdmins(Admin params) {
        Map<String, Object> dynamicSql = getDynamicSqlParams(params);
        String sql = (String) dynamicSql.get("sql");
        List<String> paramList = (List<String>) dynamicSql.get("params");
        System.out.println(sql);
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = runner.query(sql, new BeanListHandler<Admin>(Admin.class),
                    paramList.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    @Override
    public void addAdmin(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("insert into admin values(null, ?, ?, ?)",
                    admin.getEmail(),
                    admin.getNickname(),
                    admin.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Admin getAdminInfo(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Admin admin = null;
        try {
            admin = runner.query("select * from admin where id = ?", new BeanHandler<>(Admin.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public void updateAdmin(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("update admin set email = ?, nickname = ?, pwd = ? where id = ?",
                    admin.getEmail(),
                    admin.getNickname(),
                    admin.getPwd(),
                    admin.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 拼出动态sql语句以及对应的参数
     * @param params
     * @return
     */
    private Map<String, Object> getDynamicSqlParams(Admin params) {
        String baseSql = "select * from admin where 1 = 1 ";
        List<String> list = new ArrayList<>();
        if(!StringUtils.isEmpty(params.getEmail())){
            baseSql = baseSql + " and email like ?";
            list.add("%" + params.getEmail() + "%");
        }
        if(!StringUtils.isEmpty(params.getNickname())){
            baseSql = baseSql + " and nickname like ?";
            list.add("%" + params.getNickname() + "%");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("sql", baseSql);
        map.put("params", list);
        return map;
    }
}
